package fule.com.playandroidkotlin.ui.adapter

import android.app.ActivityOptions
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout
import fule.com.playandroidkotlin.R
import fule.com.playandroidkotlin.ui.model.ArticlesBean
import fule.com.playandroidkotlin.ui.model.NavigationModel
import fule.com.playandroidkotlin.ui.util.AppConstant
import fule.com.playandroidkotlin.ui.util.CommonUtil
import fule.com.playandroidkotlin.ui.util.DisplayManager

/**
 * 作者： njb
 * 时间： 2018/8/11 0011-上午 9:17
 * 描述： 导航内容适配器
 * 来源：
 */
class NavigationAdapter(data: List<NavigationModel.DataBean>?) : BaseQuickAdapter<NavigationModel.DataBean, BaseViewHolder>(R.layout.item_navigation_content, data) {
    override fun convert(helper: BaseViewHolder, item: NavigationModel.DataBean?) {
        helper.setText(R.id.title_tv, item!!.name)
        val flowLayout: TagFlowLayout = helper.getView(R.id.flow_layout)
        val articles: List<ArticlesBean> = item.articles!!
        flowLayout.run {
            adapter = object : TagAdapter<ArticlesBean>(articles) {
                override fun getView(parent: FlowLayout?, position: Int, article: ArticlesBean?): View? {

                    val tv: TextView = LayoutInflater.from(parent?.context).inflate(R.layout.flow_layout_tv,
                            flowLayout, false) as TextView

                    article ?: return null
                    val padding: Int = DisplayManager.dip2px(10F)!!
                    tv.setPadding(padding, padding, padding, padding)
                    tv.text = article.title
                    tv.setTextColor(CommonUtil.randomColor())

                    this@run.setOnTagClickListener { view, position1, _ ->
                        val options: ActivityOptions = ActivityOptions.makeScaleUpAnimation(view,
                                view.width / 2,
                                view.height / 2,
                                0,
                                0)
                        Intent(context, fule.com.playandroidkotlin.ui.activity.WebViewActivity::class.java).run {
                            //跳转到网页的url
                            putExtra(AppConstant.WEBVIEW_URL_KEY, articles[position1].link)
                            //标题
                            putExtra(AppConstant.WEBVIEW_TITLE_KEY, articles[position1].title)
                            //id
                            putExtra(AppConstant.WEBVIEW_ID_KEY, articles[position1].id)
                            context.startActivity(this, options.toBundle())
                        }
                        true
                    }
                    return tv
                }
            }
        }
    }

}